package hot100;

import comment.TreeNode;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-14  13:17
 * @Description: 将有序的数组转换为平衡二叉搜索树
 * @Version: 1.0
 */
public class hot_108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        TreeNode root=new TreeNode();
        dfs(nums,0,nums.length-1,root);
        return root;
    }
    public void dfs(int[] nums,int left,int right,TreeNode node){

        if(left>right){
            return;
        }
        int mid=(left+right)/2;

        node.val=nums[mid];
        if(left<mid){
            node.left=new TreeNode();
            dfs(nums,left,mid-1,node.left);
        }
        if(right>mid){
            node.right=new TreeNode();
            dfs(nums,mid+1,right,node.right);
        }

    }


}
